﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ShopFine.Models
{
    public class ProductDAO
    {
        ShopFineEntities db;
        public ProductDAO()
        {
            db = new ShopFineEntities();
        }

        internal List<Product> GetFeaturedList()
        {
            return db.Products.ToList();
        }

        internal Product GetProductByID(int productID)
        {
            return db.Products.FirstOrDefault(p=>p.ID == productID);
        }

        internal List<ProductModel> GetAll()
        {
            var list = db.Products.ToList();
            var result = new List<ProductModel>();
            list.ForEach(p => {
                var model = new ProductModel();
                model.CreateDate = p.CreateDate;
                model.Description = p.Description;
                model.ID = p.ID;
                model.ImageUrl = p.ImageUrl;
                model.ImageUrl1 = p.ImageURLs.Count() < 1 ? string.Empty : p.ImageURLs.OrderBy(i => i.ID).ToList().Take(1).LastOrDefault().Url;
                model.ImageUrl2 = p.ImageURLs.Count() < 2 ? string.Empty : p.ImageURLs.OrderBy(i => i.ID).ToList().Take(2).LastOrDefault().Url;
                model.ImageUrl3 = p.ImageURLs.Count() < 3 ? string.Empty : p.ImageURLs.OrderBy(i => i.ID).ToList().Take(3).LastOrDefault().Url;
                model.ImageUrl4 = p.ImageURLs.Count() < 4 ? string.Empty : p.ImageURLs.OrderBy(i => i.ID).ToList().Take(4).LastOrDefault().Url;
                model.IsActive = p.IsActive;
                model.Name = p.Name;
                model.Price = p.Price;
                model.ProductCode = p.ProductCode;
                model.SubCategoryID = p.SubCategoryID;
                model.SubCategoryName = p.SubCategory == null ? string.Empty : p.SubCategory.Name;
                model.SupplierID = p.SupplierID;
                model.SupplierName = p.Supplier == null ? string.Empty : p.Supplier.Name;
                result.Add(model);
            });
            
            return result.OrderByDescending(p => p.CreateDate).ToList();
        }

        internal bool CheckProductCodeExisted(Product insertObj)
        {
            return db.Products.Any(p=>p.ID != insertObj.ID && (p.ProductCode.Equals(insertObj.ProductCode) && !string.IsNullOrEmpty(insertObj.ProductCode)));
        }

        internal bool Insert(Product insertObj)
        {
            try
            {
                db.Products.Add(insertObj);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        internal bool Update(Product updateObj)
        {
            try
            {
                var existed = db.Products.FirstOrDefault(p => p.ID == updateObj.ID);
                if (existed != null)
                {
                    existed.ImageUrl = updateObj.ImageUrl;
                    existed.Description = updateObj.Description;
                    existed.Name = updateObj.Name;
                    existed.Price = updateObj.Price;
                    existed.SubCategoryID = updateObj.SubCategoryID;
                    existed.SupplierID = updateObj.SupplierID;
                    existed.ImageURLs = updateObj.ImageURLs;

                    db.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        internal object GetProductBySubCateID(int subID)
        {
            return db.Products.Where(p=>p.SubCategoryID.Value.Equals(subID)).ToList();
        }
    }
}